package com.example.testapp.activity.activity.study;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.example.testapp.R;
import com.example.testapp.activity.model.AddressDtailsEntity;
import com.example.testapp.activity.model.AddressModel;
import com.example.testapp.activity.util.JsonUtil;
import com.example.testapp.activity.util.Utils;
import com.example.testapp.activity.view.wheelview.ChooseAddressWheel;
import com.example.testapp.activity.view.wheelview.OnAddressChangeListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by l on 2016/7/17.
 */
public class StudyActivity extends AppCompatActivity implements OnAddressChangeListener {

    @Bind(R.id.choose_address)
    TextView  chooseAddress;

    private ChooseAddressWheel chooseAddressWheel = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_study);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        initWheel();
        initData();
    }

    private void initWheel() {
       chooseAddressWheel = new ChooseAddressWheel(this);
       chooseAddressWheel.setOnAddressChangeListener(this);
    }

    private void initData() {
        String address = Utils.readAssert(this, "address.txt");
        AddressModel model = JsonUtil.parseJson(address, AddressModel.class);
            if (model != null) {
                AddressDtailsEntity data = model.Result;
                if (data == null)
                    return;
                chooseAddress.setText(data.Province + " " + data.City + " " + data.Area);
                if (data.ProvinceItems != null && data.ProvinceItems.Province != null) {
                    chooseAddressWheel.setProvince(data.ProvinceItems.Province);
                    chooseAddressWheel.defaultValue(data.Province, data.City, data.Area);
                }
            }
    }

    @OnClick(R.id.choose_address)
    public void addressClick(View view){
        Utils.hideKeyBoard(this);
        chooseAddressWheel.show(view);
    }

    @Override
    public void onAddressChange(String province, String city, String district) {
         chooseAddress.setText(province + " " + city + " " + district);
    }
}
